package com.jiangyg.mall.member.controller;

import com.jiangyg.mall.authz.MemberInfoHolder;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.ConfuseUtils;
import com.jiangyg.mall.member.dto.MemberReceiveAddressDTO;
import com.jiangyg.mall.member.service.MemberReceiveAddressService;
import com.jiangyg.mall.member.vo.MemberReceiveAddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：会员收货地址控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:33:08
 */
@RestController
@RequestMapping("/member/receive-address")
public class MemberReceiveAddressController {

    /**
     * 会员收货地址接口
     */
    private final MemberReceiveAddressService memberReceiveAddressService;

    @Autowired
    public MemberReceiveAddressController(MemberReceiveAddressService memberReceiveAddressService) {
        this.memberReceiveAddressService = memberReceiveAddressService;
    }

    // TODO 权限校验不完善，有些请求只能后台管理调用，需要根据 cid 针对端点的校验，可以考虑 authz 模块实现

    /**
     * 功能描述：会员收货地址分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页会员收货地址
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<MemberReceiveAddressVO>> select(HttpServletRequest request, @RequestBody Pagination<MemberReceiveAddressVO> pagination) throws Exception {
        return Result.ok(memberReceiveAddressService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改会员收货地址
     *
     * @param request 请求
     * @param dto     会员收货地址
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated MemberReceiveAddressDTO dto) throws Exception {
        final Long memberId = MemberInfoHolder.loginMemberId();
        final MemberReceiveAddressVO address = memberReceiveAddressService.saveOrUpdate(dto, memberId);
        address.setPhone(ConfuseUtils.phone(address.getPhone()));
        return Result.ok(address);
    }

    /**
     * 功能描述：根据ID获取会员收货地址
     *
     * @param request                请求
     * @param receiveAddressId 会员收货地址ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<MemberReceiveAddressVO> info(HttpServletRequest request, @RequestParam(name = "receiveAddressId") Long receiveAddressId) throws Exception {
        final Long memberId = MemberInfoHolder.loginMemberId();
        return Result.ok(memberReceiveAddressService.selectById(receiveAddressId, memberId));
    }

    /**
     * 功能描述：删除会员收货地址
     *
     * @param request                 请求
     * @param receiveAddressIds 批量删除的会员收货地址ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> receiveAddressIds) {
        final Long memberId = MemberInfoHolder.loginMemberId();
        return Result.delete(memberReceiveAddressService.delete(receiveAddressIds, memberId));
    }

}
